package com.fruit.controller.wap.user;
  
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;

import com.fruit.controller.base.BaseController;
import com.fruit.pojo.address.Address;
import com.fruit.pojo.common.Page;
import com.fruit.pojo.user.UserInfo;
import com.fruit.sdk.account.useractivity.UserActivityService;
import com.fruit.sdk.account.usercount.UserCountService;
import com.fruit.service.promotion.Promotion;
import com.fruit.service.rights.UserRightsManager;
import com.fruit.service.sysmsg.SysMessageService;
import com.fruit.service.user.UserManager;
import com.fruit.util.CommonUtil;
import com.fruit.util.CustomUtil;
import com.fruit.util.MessagesCode;
import com.fruit.util.ResultInfo;

import jodd.util.StringUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 用户相关
 * 
 * @author Administrator
 * @version 2017.11.30 17:40
 */
@Slf4j
@Controller
@RequestMapping(value = "/wap/usercount")
public class UserControllerWap extends BaseController {
	@Autowired
	private UserCountService userCountService;
	
	@Autowired
	private UserManager userService;
	
	@Autowired
	private UserRightsManager userRightsManager;
	
	@Autowired
	private SysMessageService sysMessageService;	
	@Autowired
	private Promotion promotionService;
	@Autowired
	private UserActivityService userActivityService;
	
    @Value("${h5_domain}")
    private String h5Domain;
	
	/**
	 * 获取用户佣金统计金额
	 * @param userId userId
	 * @param page page
	 * @param request request
	 * @param session session
	 * @return 结果
	 */
	@RequestMapping(value = "/countUserCommissions", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Map<String, Object> countUserCommissions(@SessionAttribute("userId")Integer userId, Page page, HttpServletRequest request, HttpSession session) {
		log.info("【用户佣金统计】---【开始】--userId={}", userId);
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			if (userId == null) { //用户标识
				result.put("result", this.initResult(ResultInfo.ERROR, "param", "用户id不能为空"));
				return result;
			}
			//1、统计用户下级贡献佣金明细及总金额
			Map<String, Object> pam = new HashMap<String, Object>();
			pam.put("start", (page.getPage() - 1) * page.getShowNum());
			pam.put("num", page.getShowNum());
			pam.put("userId", userId);
			Map<String, Object> res = userCountService.countUserCommissions(pam);
			ResultInfo commResult = (ResultInfo) res.get("result");
			if (ResultInfo.ERROR.equals(commResult.getState())) {
				result.put("result", commResult);
				return result;
			}
			
			//2、用户直属下级数量
			Map<String, Object> userRes = userService.getDownUserNum(userId);
			ResultInfo userResult = (ResultInfo) userRes.get("result");
			if (ResultInfo.ERROR.equals(userResult.getState())) {
				result.put("result", userResult);
				return result;
			}
			//3、当前用户的上级
			UserInfo userPanent = userService.getParentOne(userId);
			if(userPanent == null){
				result.put("userParent", null); //用户上级
			} else {
				result.put("userParent", userPanent.getPetName()); //用户上级
			}
			int goldBowlCnt = promotionService.getCount(userId, "gold_bowl");
			boolean buyState = userService.getUserBuyState(userId);
			result.put("goldBowlCnt", goldBowlCnt);//金饭碗数量
			result.put("dataList", res.get("dataList")); //佣金明细
			result.put("money", res.get("money")); //总金额
			result.put("downUser", userRes.get("downUser")); //用户数量
			result.put("buyState", buyState); //用户数量
			result.put("result", this.initResult(ResultInfo.SUCCESS, "ok", "操作成功！"));
		} catch (Exception e) {
			log.info("【用户佣金统计】---【异常】---{}", e);
			result.put("result", this.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		log.info("【用户佣金统计】---【结束】--");
		return result;
	}
	
	/**
	 * 邀请码绑定上级
	 * @param userId userId
	 * @param request request
	 * @param code code
	 * @return 结果
	 */
	@RequestMapping(value = "/invitation", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Map<String, Object> invitation(@SessionAttribute("userId")Integer userId, String code) {
		log.info("【邀请码绑定上级】---【开始】--userId={}", userId);
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			if (userId == null || code == null) { //用户标识
				result.put("result", this.initResult(ResultInfo.ERROR, "param", "参数错误"));
				return result;
			}
			result = userService.saveSuperior(userId,code);
		} catch (Exception e) {
			log.info("【邀请码绑定上级】---【异常】---{}", e);
			result.put("result", this.initResult(ResultInfo.ERROR, "exception", "网络异常"));
		}
		log.info("【邀请码绑定上级】---【结束】--");
		return result;
	}
	
	 /**
     * 收货地址信息
     *
     * @param userId userId
     * @return Map<String, Object>
     */
    @RequestMapping(value = "address/list", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Map<String, Object> addressList(@SessionAttribute("userId")Integer userId) {
        this.logBefore(logger, "收货地址信息");
        Map<String, Object> resMap = new HashMap<String, Object>();
        if (userId == null) {
            resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.MESS_OUT, "参数不全!"));
            return resMap;
        }
        try {
			List<Address> address = userService.getAddressList(userId);
            if (address.size() > 0) {
                resMap.put("result", this.initResult(ResultInfo.SUCCESS, "SUCCESS", "查询成功!"));
                resMap.put("address", address);
            } else {
                resMap.put("result", this.initResult(ResultInfo.SUCCESS, MessagesCode.MESS_ISnULL, "还未设置收货地址!"));
            }
        } catch (Exception e) {
            this.logError(logger, e.getMessage());
            resMap.put("result", this.initResult(ResultInfo.ERROR, "expection", "系统繁忙!"));
        }
        return resMap;
    }
    
	 /**
     * 收货地址信息
     *
     * @param parentId parentId
     * @return Map<String, Object>
     */
    @RequestMapping(value = "getRegion", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Map<String, Object> getRegion(Integer parentId) {
        this.logBefore(logger, "收货地址信息");
        Map<String, Object> resMap = new HashMap<String, Object>();
        if (parentId == null) {
            resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.MESS_OUT, "参数不全!"));
            return resMap;
        }
        try {
			List<Map<String, Object>> region = userService.getRegion(parentId);
            if (region.size() > 0) {
                resMap.put("result", this.initResult(ResultInfo.SUCCESS, "SUCCESS", "查询成功!"));
                resMap.put("region", region);
            } else {
                resMap.put("result", this.initResult(ResultInfo.SUCCESS, MessagesCode.MESS_ISnULL, "未查询到区域!"));
            }
        } catch (Exception e) {
            this.logError(logger, e.getMessage());
            resMap.put("result", this.initResult(ResultInfo.ERROR, "expection", "网络繁忙!"));
        }
        return resMap;
    }
    
    /**
     * 收货地址信息
     *
     * @param parentId parentId
     * @return Map<String, Object>
     */
    @RequestMapping(value = "getRegions", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Map<String, Object> getRegions() {
        this.logBefore(logger, "收货地址信息");
        Map<String, Object> resMap = new HashMap<String, Object>();
        try {
			List<Map<String, Object>> region = userService.getRegions();
            if (region.size() > 0) {
                resMap.put("result", this.initResult(ResultInfo.SUCCESS, "SUCCESS", "查询成功!"));
                resMap.put("region", region);
            } else {
                resMap.put("result", this.initResult(ResultInfo.SUCCESS, MessagesCode.MESS_ISnULL, "未查询到区域!"));
            }
        } catch (Exception e) {
            this.logError(logger, e.getMessage());
            resMap.put("result", this.initResult(ResultInfo.ERROR, "expection", "网络繁忙!"));
        }
        return resMap;
    }

    /**
     * 添加收货地址
     *
     * @param address address
     * @return Map<String, Object>
     */
    @RequestMapping(value = "address/save", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Map<String, Object> saveAddress(@SessionAttribute("userId")Integer userId, Address address) {
        this.logBefore(logger, "添加收货地址");
        Map<String, Object> resMap = new HashMap<String, Object>();
        try {
            if (address == null || userId == null) {
                resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.MESS_OUT, "参数不全!"));
            } else {
            	address.setUserId(userId);
                List<Address> addres = userService.getAddressList(address.getUserId());
                if (addres != null) {
                    for (Address addre : addres) {
                        if ("01".equals(address.getStatus())) {
                            if ("01".equals(addre.getStatus())) {
                                addre.setStatus("00");
                                userService.updateAddStatus(addre);
                            }
                        }
                    }
                }
                Address addre = userService.saveAddress(address);
                if (addre != null) {
                    resMap.put("result", this.initResult(ResultInfo.SUCCESS, "SUCCESS", "添加成功!"));
                    resMap.put("address", addre);
                }

            }
        } catch (Exception e) {
            this.logError(logger, e.getMessage());
            resMap.put("result", this.initResult(ResultInfo.ERROR, "expection", "系统繁忙!"));
        }
        return resMap;
    }

    /**
     * 修改收货地址
     *
     * @param address address
     * @return Map<String, Object>
     */
    @RequestMapping(value = "address/update", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Map<String, Object> updateAddress(@SessionAttribute("userId")Integer userId, Address address) {
        this.logBefore(logger, "修改收货地址");
        Map<String, Object> resMap = new HashMap<String, Object>();
        try {
            if (address == null || userId == null) {
                resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.MESS_OUT, "参数不全!"));
            } else {
            	address.setUserId(userId);
                if ("01".equals(address.getStatus())) {
                    List<Address> addres = userService.getAddressList(address.getUserId());
                    if (addres != null) {
                        for (Address addre : addres) {
                            if ("01".equals(address.getStatus())) {
                                if ("01".equals(addre.getStatus())) {
                                    addre.setStatus("00");
                                    userService.updateAddStatus(addre);
                                }
                            }
                        }
                    }
                }
                userService.updateAddress(address);
                Address addre = userService.getAddressById(address.getId());
                if (addre != null) {
                    resMap.put("result", this.initResult(ResultInfo.SUCCESS, "SUCCESS", "修改成功!"));
                    resMap.put("address", addre);
                }
            }
        } catch (Exception e) {
            this.logError(logger, e.getMessage());
            resMap.put("result", this.initResult(ResultInfo.ERROR, "expection", "系统繁忙!"));
        }
        return resMap;
    }

    /**
     * 收货地址信息
     *
     * @param addressId addressId
     * @return Map<String, Object>
     */
    @RequestMapping(value = "addressMsg", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Map<String, Object> addressMsg(String addressId) {
        this.logBefore(logger, "收货地址信息");
        Map<String, Object> resMap = new HashMap<String, Object>();
        if (StringUtils.isEmpty(addressId)) {
            resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.MESS_OUT, "参数不全!"));
            return resMap;
        }
        try {
            Address address = userService.getAddressById(Integer.parseInt(addressId));
            if (address != null) {
                resMap.put("result", this.initResult(ResultInfo.SUCCESS, "SUCCESS", "查询成功!"));
                resMap.put("address", address);
            } else {
                resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.MESS_ISnULL, "未找到收货地址!"));
            }
        } catch (Exception e) {
            this.logError(logger, e.getMessage());
            resMap.put("result", this.initResult(ResultInfo.ERROR, "expection", "系统繁忙!"));
        }
        return resMap;
    }

    /**
     * 修改默认地址状态
     *
     * @param addressId addressId
     * @param userId    userId
     * @return Map<String, Object>
     */
    @RequestMapping(value = "address/updateStatus", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Map<String, Object> updateStatus(@SessionAttribute("userId")Integer userId, String addressId) {
        this.logBefore(logger, "修改默认地址状态");
        Map<String, Object> resMap = new HashMap<String, Object>();
        if (StringUtils.isEmpty(addressId) || userId == null) {
            resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.MESS_OUT, "参数不全!"));
            return resMap;
        }
        try {
            List<Address> addres = userService.getAddressList(userId);
            if (addres != null) {
                for (Address addre : addres) {
                    if ("01".equals(addre.getStatus())) {
                        addre.setStatus("00");
                        userService.updateAddStatus(addre);
                    }
                }
            }
            Address add = new Address();
            add.setStatus("01");
            add.setId(Integer.parseInt(addressId));
            userService.updateAddStatus(add);
            List<Address> addr = userService.getAddressList(userId);
            if (addr != null) {
                resMap.put("result", this.initResult(ResultInfo.SUCCESS, "SUCCESS", "修改成功!"));
                resMap.put("address", addr);
            }
        } catch (Exception e) {
            this.logError(logger, e.getMessage());
            resMap.put("result", this.initResult(ResultInfo.ERROR, "expection", "系统繁忙!"));
        }
        return resMap;
    }

    /**
     * 删除收货地址
     *
     * @param addressId addressId
     * @return Map<String, Object>
     */
    @RequestMapping(value = "address/delete", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Map<String, Object> deleteAddress(String addressId) {
        this.logBefore(logger, "删除收货地址");
        Map<String, Object> resMap = new HashMap<String, Object>();
        if (StringUtils.isEmpty(addressId)) {
            resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.MESS_OUT, "参数不全!"));
            return resMap;
        }
        try {
            int num = userService.deleteAddress(Integer.parseInt(addressId));
            if (num == 0) {
                resMap.put("result", this.initResult(ResultInfo.ERROR, "ERROR", "删除失败!"));
            } else {
                resMap.put("result", this.initResult(ResultInfo.SUCCESS, "SUCCESS", "删除成功!"));
            }
        } catch (Exception e) {
            this.logError(logger, e.getMessage());
            resMap.put("result", this.initResult(ResultInfo.ERROR, "expection", "系统繁忙!"));
        }
        return resMap;
    }
    
    /**
     * 通过用户id获取用户信息
     *
     * @param userId userId
     * @return Map<String, Object>
     */
    @RequestMapping(value = "userCenter", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Map<String, Object> userCenter(@SessionAttribute("userId")Integer userId) {
        this.logBefore(logger, "用户信息");
        Map<String, Object> resMap = new HashMap<String, Object>();
        Map<String, Object> pam = new HashMap<String, Object>();
        if (userId == null) {
            resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.MESS_OUT, "参数不全!"));
            return resMap;
        }
        try {
            UserInfo user = userService.getUser(userId);
            if (user != null) {
                int rightNum = userRightsManager.getRightNum(user.getId());
                pam.put("userId", userId);
    			ResultInfo res = sysMessageService.selectUnReadCntByUserId(pam);
                resMap.put("result", this.initResult(ResultInfo.SUCCESS, "SUCCESS", "查询成功!"));
                resMap.put("userInfo", user);
                resMap.put("rightNum", rightNum);
                resMap.put("msgNum", res.getObj());
            } else {
                resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.EXIST, "未找到用户!"));
            }
        } catch (Exception e) {
            this.logError(logger, e.getMessage());
            resMap.put("result", this.initResult(ResultInfo.ERROR, "expection", "系统繁忙!"));
        }
        return resMap;
    }
    
    
    /**
     * app头像修改
     *
     * @param userId  userId
     * @param request request
     * @return Map<String, Object>
     */
    @RequestMapping(value = "headImage", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Map<String, Object> groupImage(@SessionAttribute("userId")Integer userId, HttpServletRequest request,String headUrl) {
        this.logBefore(logger, "头像修改");
        Map<String, Object> resMap = new HashMap<String, Object>();
        if (userId == null || StringUtils.isEmpty(headUrl)) {
            resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.MESS_OUT, "参数不全!"));
            return resMap;
        }
        /*CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
                request.getSession().getServletContext());*/
        /*MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultipartFile file = multipartRequest.getFile("file");
        InputStream in = null;
        try {
            in = file.getInputStream();
        } catch (IOException e1) {
            e1.printStackTrace();
        }*/
        try {
           /* UserInfo user = userService.getUser(userId);
            String path = "fruit/user/" + userId + "/head.png";
            Map imagePath = pictureService.uploadPicture(path, IOUtils.read(in, 1024));
            if (imagePath.size() == 0) {
                resMap.put("result", this.initResult(ResultInfo.ERROR, "error", "修改失败!"));
                return resMap;
            }
            int state = (int) imagePath.get("error");*/
          //  if (0 == state) {
              //  String imageAddress = (String) imagePath.get("url");
        	    UserInfo user = userService.getUser(userId);
        	    if(user==null){
        	    	resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.ERROR, "用户不存在!"));
                    return resMap;
        	    }
                user.setHeadImage(headUrl + "?v" + CustomUtil.buildRandom(4));
                int num = userService.updateUserHeadImage(user);
                if (num == 0) {
                    resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.ERROR, "修改失败!"));
                    return resMap;
                }
                UserInfo use = userService.getUser(userId);
                if (use != null) {
                    resMap.put("result", this.initResult(ResultInfo.SUCCESS, MessagesCode.SUCCESS, "修改成功!"));
                    resMap.put("userInfo", use);
                }
            /*} else {
                resMap.put("result", this.initResult(ResultInfo.ERROR, "error", "修改失败!"));
                return resMap;
            }*/
        } catch (Exception e) {
            e.printStackTrace();
            this.logError(logger, e.getMessage());
            resMap.put("result", this.initResult(ResultInfo.ERROR, "expection", "系统繁忙!"));
        }
        return resMap;
    }
    
    /**
     * app用户信息修改 昵称 ,性别
     *
     * @param userId userId
     * @param user   user
     * @return Map<String, Object>
     */
    @RequestMapping(value = "userAlter", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Map<String, Object> appUpdateUser(@SessionAttribute("userId")Integer userId, UserInfo user) {
        this.logBefore(logger, "用户修改");
        Map<String, Object> resMap = new HashMap<String, Object>();
        try {
            if (user == null || userId == null) {
                resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.MESS_OUT, "参数不全!"));
                return resMap;
            }
            UserInfo us = userService.getUser(userId);
            if (us == null) {
                resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.EXIST, "用户不存在!"));
                return resMap;
            }
            user.setVersion(us.getVersion());
            user.setId(userId);
            int num = userService.updateUserMess(user);
            if (num == 0) {
                resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.ERROR, "修改失败"));
                return resMap;
            } 
            UserInfo use = userService.getUser(userId);
            if (use != null) {
                resMap.put("result", this.initResult(ResultInfo.SUCCESS, MessagesCode.SUCCESS, "修改成功"));
                resMap.put("userInfo", use);
            }
        } catch (Exception e) {
            e.printStackTrace();
            this.logError(logger, e.getMessage());
            if (user.getPetName() != null && user.getPetName() != "") {
            	resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.ERROR, "修改失败"));
            	resMap.put("updateState","01");
            }else{
            	resMap.put("result", this.initResult(ResultInfo.ERROR, "expection", "网络繁忙!"));
            }
        }
        return resMap;
    }
    
    /**
     * 修改密码
     *
     * @param request request
     * @return Map<String, Object>
     */
    @RequestMapping(value = "updateUserPwd", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Map<String, Object> updateUserPwd(@SessionAttribute("userId")Integer userId, HttpServletRequest request) {
        this.logBefore(logger, "修改密码");
        Map<String, Object> resMap = new HashMap<String, Object>();
        String oldPwd = request.getParameter("oldPwd");
        String newPwd = request.getParameter("newPwd");
        try {
            if (userId == null || StringUtils.isEmpty(oldPwd) || StringUtils.isEmpty(newPwd)) {
                resMap.put("result", this.initResult(ResultInfo.ERROR, "no-param", "参数不全!"));
                return resMap;
            }
            resMap = userService.updatePwd(Integer.toString(userId), oldPwd, newPwd);
        } catch (Exception e) {
            e.printStackTrace();
            this.logError(logger, e.getMessage());
            resMap.put("result", this.initResult(ResultInfo.ERROR, "expection", "网络繁忙!"));
        }
        return resMap;
    }

    
    /**
     * 获取注册
     *
     * @param request request
     * @return Map<String, Object>
     */
    @RequestMapping(value = "getRegister", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Map<String, Object> getRegister(@SessionAttribute("userId")Integer userId, HttpServletRequest request) {
        this.logBefore(logger, "获取注册");
        Map<String, Object> resMap = new HashMap<String, Object>();
        if (userId == null) {
            resMap.put("result", this.initResult(ResultInfo.ERROR, "no-param", "参数不全!"));
            return resMap;
        }
        try {
            UserInfo ui = userService.getUser(userId);
            if (ui != null) {
                String vipcode = ui.getCodePre() + userId + ui.getCodeSuf();
                String href = h5Domain + "/inviteLook?code=" + vipcode;
                resMap.put("href", href);
                resMap.put("result", this.initResult(ResultInfo.SUCCESS, "SUCCESS", "成功!"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            this.logError(logger, e.getMessage());
            resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.SYSTEM, "系统繁忙!"));
        }
        this.logAfter(logger);
        return resMap;
    }
    
    
	/**
	 * 获取活动商品
	 * @param code code
	 * @return 结果
	 */
	@RequestMapping(value = "/getActivityGoods", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Map<String, Object> getActivityGoods(String code,Page page) {
		log.info("【获取活动商品】---【开始】");
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			if (StringUtil.isEmpty(code)) {
				result.put("result", this.initResult(ResultInfo.ERROR, "param", "参数错误"));
				return result;
			}
			Map<String, Object> pam = new HashMap<String, Object>();
			pam.put("start", (page.getPage() - 1) * page.getShowNum());
			pam.put("num", page.getShowNum());
			pam.put("code", code);
			List<Map<String,Object>> goods= userService.getActivityGoods(pam);
			result.put("goods", goods);
			result.put("result", this.initResult(ResultInfo.SUCCESS, "ok", "获取成功！"));
		} catch (Exception e) {
			log.info("【获取活动商品】---【异常】---{}", e);
			result.put("result", this.initResult(ResultInfo.ERROR, "exception", "网络异常"));
		}
		log.info("【获取活动商品】---【结束】--");
		return result;
	}
	/**
	 * 获取当前活动用户信息
	 * @param code 活动编码
	 * @param userId 用户标识
	 * @param request request
	 * @return Map<String, Object>
	 */
    @RequestMapping(value = "/getActivityUserInfo", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Map<String, Object> getActivityUserInfo(String code, @SessionAttribute("userId")Integer userId, HttpServletRequest request) {
        log.info("【获取当前活动用户信息】------【开始】------code={}", code);
        Map<String, Object> result = new HashMap<String, Object>();
        try {
        	if (StringUtils.isEmpty(code)) { //活动编码
    			result.put("result", this.initResult(ResultInfo.ERROR, "param", "活动编码不能为空"));
    			return result;
    		}
        	if (userId == null) { //用户标识
    			result.put("result", this.initResult(ResultInfo.ERROR, "param", "用户id不能为空"));
    			return result;
    		}
        	String tradeId = CommonUtil.getUUID();
        	result = userActivityService.getUserActivityInfos(tradeId, userId, code);
        } catch (Exception e) {
        	log.info("【获取当前活动用户信息】------【异常】------{}", e);
        	result.put("result", this.initResult(ResultInfo.ERROR, "exception", "系统繁忙!"));
        }
        log.info("【获取当前活动用户信息】------【结束】------{}", result.toString());
        return result;
    }
    /**
	 * 检查当前用户是否参与活动
	 * @param code 活动编码
	 * @param userId 用户标识
	 * @param request request
	 * @return Map<String, Object>
	 */
    @RequestMapping(value = "/checkActivityUserInfo", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Map<String, Object> checkActivityUserInfo(String code, @SessionAttribute("userId")Integer userId, HttpServletRequest request) {
        log.info("【检查当前用户是否参与活动】------【开始】------code={}", code);
        Map<String, Object> result = new HashMap<String, Object>();
        try {
        	if (StringUtils.isEmpty(code)) { //活动编码
    			result.put("result", this.initResult(ResultInfo.ERROR, "param", "活动编码不能为空"));
    			return result;
    		}
        	if (userId == null) { //用户标识
    			result.put("result", this.initResult(ResultInfo.ERROR, "param", "用户id不能为空"));
    			return result;
    		}
        	String tradeId = CommonUtil.getUUID();
        	result = userActivityService.checkUserActivityInfos(tradeId, userId, code);
        } catch (Exception e) {
        	log.info("【checkUserActivityInfos】------【异常】------{}", e);
        	result.put("result", this.initResult(ResultInfo.ERROR, "exception", "系统繁忙!"));
        }
        log.info("【checkUserActivityInfos】------【结束】------{}", result.toString());
        return result;
    }
    
    /**
	 * 用户粮票明细
	 * @param page 分页参数
	 * @param id 用户标识
	 * @param request request
	 * @return Map<String, Object>
	 */
    @RequestMapping(value = "/getUserTicketDetail", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Map<String, Object> getUserTicketDetail(Integer id, HttpServletRequest request) {
        log.info("【用户粮票明细】------【开始】------id={}", id);
        Map<String, Object> result = new HashMap<String, Object>();
        try {
        	if (id == null) { //主键id
    			result.put("result", this.initResult(ResultInfo.ERROR, "param", "id不能为空"));
    			return result;
    		}
        	String tradeId = CommonUtil.getUUID();
        	Map<String, Object> params = new HashMap<String, Object>();
        	params.put("id", id);
        	log.info("【用户粮票明细】------【参数】------id={}", id);
        	result = userCountService.getUserTicketDetail(tradeId, params);
        } catch (Exception e) {
        	log.info("【用户粮票明细】------【异常】------{}", e);
        	result.put("result", this.initResult(ResultInfo.ERROR, "exception", "系统繁忙!"));
        }
        log.info("【用户粮票明细】------【结束】------{}", result.toString());
        return result;
    }
    
    /**
	 * 用户粮票明细列表
	 * @param page 分页参数
	 * @param userId 用户标识
	 * @param request request
	 * @return Map<String, Object>
	 */
    @RequestMapping(value = "/getUserTicketList", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Map<String, Object> getUserTicketList(Page page,@SessionAttribute("userId")Integer userId, HttpServletRequest request) {
        log.info("【用户粮票明细列表】------【开始】------userId={}", userId);
        Map<String, Object> result = new HashMap<String, Object>();
        try {
        	if (userId == null) { //用户标识
    			result.put("result", this.initResult(ResultInfo.ERROR, "param", "用户id不能为空"));
    			return result;
    		}
        	String tradeId = CommonUtil.getUUID();
        	Map<String, Object> params = new HashMap<String, Object>();
        	params.put("startNum",  (page.getPage() - 1) * page.getShowNum());
        	params.put("pageNum", page.getShowNum());
        	params.put("userId", userId);
        	log.info("【用户粮票明细】------【参数】------params={}", params.toString());
        	result = userCountService.getUserTicketList(tradeId, params);
        } catch (Exception e) {
        	log.info("【用户粮票明细列表】------【异常】------{}", e);
        	result.put("result", this.initResult(ResultInfo.ERROR, "exception", "系统繁忙!"));
        }
        log.info("【用户粮票明细列表】------【结束】------{}", result.toString());
        return result;
    }
    
    /**
     * 用户兑换券列表
     * @param page 分页参数
     * @param userId 用户标识
     * @param state 状态
     * @return Map<String, Object>
     */
    @RequestMapping(value = "/getUserVoucherList", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Map<String, Object> getUserVoucherList(Page page,@SessionAttribute("userId")Integer userId, String state) {
        log.info("【用户兑换券列表】------【开始】------userId={}", userId);
        Map<String, Object> result = new HashMap<String, Object>();
        try {
        	if (userId == null) { //用户标识
    			result.put("result", this.initResult(ResultInfo.ERROR, "param", "用户id不能为空"));
    			return result;
    		}
        	if (StringUtils.isEmpty(state)) { //状态
    			result.put("result", this.initResult(ResultInfo.ERROR, "param", "状态不能为空"));
    			return result;
    		}
        	String tradeId = CommonUtil.getUUID();
        	Map<String, Object> params = new HashMap<String, Object>();
        	params.put("startNum",  (page.getPage() - 1) * page.getShowNum());
        	params.put("pageNum", page.getShowNum());
        	params.put("userId", userId);
        	params.put("state", state);
        	log.info("【用户兑换券列表】------【参数】------params={}", params.toString());
        	result = userCountService.getUserVoucherList(tradeId, params);
        } catch (Exception e) {
        	log.info("【用户兑换券列表】------【异常】------{}", e);
        	result.put("result", this.initResult(ResultInfo.ERROR, "exception", "系统繁忙!"));
        }
        log.info("【用户兑换券列表】------【结束】------{}", result.toString());
        return result;
    }
}
